package com.hay.pay.support.ali;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.request.*;

/**
 * 阿里支付api
 * @author LiMouRen
 * @date 2024/1/30
 */
public class AliPayApi {

    /**
     * 三方应用-换取应用授权令牌
     * <a href="https://opendocs.alipay.com/isv/04h3uf?scene=common&pathHash=2a0adf2e"/>详见</a>
     * @param code 应用授权码
     */
    public static AlipayOpenAuthTokenAppRequest authTokenAppRequest(String code){
        AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("grant_type", "authorization_code");
        bizContent.put("code", code);
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 三方应用-获取授权应用aes
     * <a href="https://opendocs.alipay.com/isv/a368b652_alipay.open.auth.app.aes.get?scene=common&pathHash=1d59b6ad"/>详见</a>
     * @param merchantAppId 商家应用appId
     */
    public static AlipayOpenAuthAppAesGetRequest authAppAesGetRequest(String merchantAppId){
        AlipayOpenAuthAppAesGetRequest request = new AlipayOpenAuthAppAesGetRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("merchant_app_id", merchantAppId);
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 三方应用-设置授权应用aes
     * <a href="https://opendocs.alipay.com/isv/7014cef9_alipay.open.auth.app.aes.set?scene=common&pathHash=681cae96"/>详见</a>
     * @param merchantAppId 商家应用appId
     */
    public static AlipayOpenAuthAppAesSetRequest authAppAesSetRequest(String merchantAppId){
        AlipayOpenAuthAppAesSetRequest request = new AlipayOpenAuthAppAesSetRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("merchant_app_id", merchantAppId);
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 查询订单参数
     * <a href="https://opendocs.alipay.com/mini/05xsky?scene=common&pathHash=354e8be3"/>详见</a>
     * @param orderCode 商户订单号
     */
    public static AlipayTradeQueryRequest tradeQueryRequest(String orderCode){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderCode);
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 关闭订单参数
     * <a href="https://opendocs.alipay.com/mini/05xunj?scene=common&pathHash=ca2a9ea6"/>详见</a>
     * @param orderCode 商户订单号
     */
    public static AlipayTradeCancelRequest tradeCancelRequest(String orderCode){
        AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderCode);
        request.setBizContent(bizContent.toString());
        return request;
    }

    /**
     * 查询退款参数,二选一
     * <a href="https://opendocs.alipay.com/mini/4a0fb7bf_alipay.trade.fastpay.refund.query?scene=common&pathHash=2e6cbb7c"/>详见</a>
     * @return
     */
    public static AlipayTradeFastpayRefundQueryRequest tradeRefundQueryRequest(String tradeNo,String outTradeNo){
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_request_no",outTradeNo);
        bizContent.put("trade_no", tradeNo);
        bizContent.put("out_trade_no", outTradeNo);
        JSONArray queryOptions = new JSONArray();
        queryOptions.add("refund_detail_item_list");
        queryOptions.add("gmt_refund_pay");
        queryOptions.add("deposit_back_info");
        bizContent.put("query_options", queryOptions);
        request.setBizContent(bizContent.toString());
        return request;
    }

}
